package com.oauth.application.server.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Required;

import com.oauth.application.client.beans.ApplicationClientBean;
import com.oauth.application.client.rpc.ApplicationRemoteService;
import com.oauth.application.server.utils.ApplicationUtil;
import com.oauth.common.server.OAuthGwtController;
import com.oauth.common.server.util.OAuthRequestUtil;
import com.oauth.service.application.ApplicationService;
import com.oauth.service.application.registry.beans.ApplicationInfoBean;
import com.oauth.service.generic.beans.OAuthServiceBean;

public class ApplicationGwtController extends OAuthGwtController implements
		ApplicationRemoteService {

	private ApplicationService mApplicationService;

	/**
	 * 
	 */
	private static final long serialVersionUID = -2118333321471056975L;

	public List<ApplicationClientBean> getMyApplications() {
		Long memberId = OAuthRequestUtil.getMemberId(getHttpRequest());

		List<ApplicationInfoBean> applicationInfo = mApplicationService
				.getSubscribedApplication(memberId);
		return ApplicationUtil.convertToClientBeans(applicationInfo);
	}

	public String subscribeToApplication(Long pApplicationId) {
		Long memberId = OAuthRequestUtil.getMemberId(getHttpRequest());

		return mApplicationService.subscribeApplication(memberId, pApplicationId) ? "success"
				: "Failed";
	}

	public String unSubscribeFromApplication(Long pApplicationId) {
		Long memberId = OAuthRequestUtil.getMemberId(getHttpRequest());
		return mApplicationService.unSubscribeApplication(memberId, pApplicationId) ? "success"
				: "Failed";
	}

	public List<ApplicationClientBean> getUnSubscribedApplications() {
		Long memberId = OAuthRequestUtil.getMemberId(getHttpRequest());

		List<ApplicationInfoBean> applicationInfo = mApplicationService
				.getUnsubscribedApplication(memberId);

		return ApplicationUtil.convertToClientBeans(applicationInfo);
	}

	@Override
	protected <T extends OAuthServiceBean> Class<T> getServiceBeanClass() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @param pApplicationService
	 *            the applicationService to set
	 */
	@Required
	public void setApplicationService(ApplicationService pApplicationService) {
		mApplicationService = pApplicationService;
	}

}
